
import xml.dom.minidom
import datetime

#####################################################################################################################################
# Artist class
class Album:
	def __init__(self, node, artist=None):
		self.artist = artist.name
		self.ID = node.getAttribute('id')
		self.type = node.getAttribute('type')
		
		for n in node.getElementsByTagName('title')[0].childNodes:
			if n.nodeType == n.TEXT_NODE:
				self.title = n.data.strip()
		

		self.year = datetime.date.today().year
		
		for nodeEvent in node.getElementsByTagName('event'):
			year = nodeEvent.getAttribute('date')
			self.year = (int(year.split('-')[0]) < self.year) and int(year.split('-')[0]) or self.year 
			
		self.year = str(self.year)

	def compare(self, album, album_request):
		self.score = distance(unicode(self.title), unicode(album_request))
		if album is None:
			return self
		if self.score == album.score: return album
		if self.score < album.score: return self
		if self.score > album.score: return album
		
	def set_album_cover(self, url):
		self.the_cover = url

	@staticmethod
	def get_cover(document):
		images = document.getElementsByTagName('image')
		if len(images) == 0:
			return DEFAULT_COVER 
		covers = {}
		for imgNode in images:
			size = imgNode.getAttribute('size')
			url = ''
			for n in imgNode.childNodes:
				if n.nodeType == n.TEXT_NODE:
					url = n.data.strip()
			covers[size] = url
		
		for s in ['small','medium','large','extralarge']:
			if  s in covers:
				the_cover = covers[s]
		return the_cover
	
	def __str__(self):
		s = ''
		s = s + 'ID : ' + self.ID + '\n'
		s = s + 'type : ' + self.type + '\n'
		s = s + 'title : ' + self.title + '\n'
		s = s + 'year : ' + self.year + '\n'
		s = s + 'score : ' + str(self.score) + '\n'
		s = s + 'artist : ' + str(self.artist)
		return s
